package com.shf.controller;

import com.shf.domain.ResponseResult;
import com.shf.domain.dto.AddTagDto;
import com.shf.domain.dto.EditTagDto;
import com.shf.domain.dto.TagListDto;
import com.shf.domain.entity.Tag;
import com.shf.domain.vo.PageVo;
import com.shf.domain.vo.TagVo;
import com.shf.service.TagService;
import com.shf.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 叶枫
 * @date 2023/11/14.
 */

@RestController
@RequestMapping("/content/tag")
public class TagController {
    @Autowired
    public TagService tagService;

    @GetMapping("/list")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, TagListDto tagListDto){

            return tagService.getTagList(pageNum,pageSize,tagListDto);



    }

    /**
     * 添加标签
     * @param tagDto
     * @return
     */
    @PostMapping
    public ResponseResult add(@RequestBody AddTagDto tagDto){
        Tag tag = BeanCopyUtils.copyBean(tagDto, Tag.class);
        tagService.save(tag);
        return ResponseResult.okResult();
    }

    /**
     * 删除标签 这个动作是 逻辑删除 并不是真正意义的删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id){
        tagService.deleteById(id);
        return ResponseResult.okResult();
    }

    /**
     *
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    //①根据标签的id来查询标签
    public ResponseResult getInfo(@PathVariable(value = "id")Long id){
        Tag tag = tagService.getById(id);
        return ResponseResult.okResult(tag);
    }

    @PutMapping
    //②根据标签的id来修改标签
    public ResponseResult edit(@RequestBody EditTagDto tagDto){
        Tag tag = BeanCopyUtils.copyBean(tagDto,Tag.class);
        tagService.updateById(tag);
        return ResponseResult.okResult();
    }

    /**
     * 写博文-查询文章标签的接口
     * @return
     */
    @GetMapping("/listAllTag")
    public ResponseResult listAllTag(){
        List<TagVo> list = tagService.listAllTag();
        return ResponseResult.okResult(list);
    }

}
